from utils import *
from moviepy.editor import VideoFileClip


def scan_mp4_files(folder):
    files = []
    for root, _, f in os.walk(folder):
        for file in f:
            if file.endswith('.mp4'):
                files.append(os.path.join(root, file))
    return files


def mp4_to_flac(mp4_file):
    mp4_file_folder = os.path.dirname(mp4_file)
    mp4_file_name = os.path.basename(mp4_file).split('.')[0]
    if if_file_exists(f'{mp4_file_folder}/{mp4_file_name}.flac'):
        print(f'File {mp4_file_name}.flac already exists')
        return
    flac_file = f'{mp4_file_folder}/{mp4_file_name}.flac'
    video = VideoFileClip(mp4_file)
    audio = video.audio
    #write to audio with sample rate of 16000
    audio.write_audiofile(flac_file, fps=16000, codec='flac')
    print(f'File {mp4_file_name}.flac created')


for mp4 in scan_mp4_files('test'):
    print(mp4)
    mp4_to_flac(mp4)
